package com.mz.mattress.api.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.mz.mattress.api.common.BaseEntity;
import com.mz.mattress.api.common.StepBean;
import com.mz.mattress.api.enums.TaskStatusEnum;
import lombok.Data;

import java.util.Collections;
import java.util.List;

//任务执行数据
@Data
@TableName("mz_plan_task_data")
public class PlanTaskData extends BaseEntity {

    private Integer id;

    /**
     * 任务类型
     * 1新注册会员，下订单 ，核销订单
     * 2老会员 ，已有订单，直接核销
     * 3老会员，下订单，核销
     */
    private Integer type;

    //执行bean
    private String beanName;

    //日期 yyyyMMdd
    private String dates;

    //总步骤
    private Integer totalStep;

    //当前步骤
    private Integer curStep;

    //状态 0未开始 1已取消 2执行中 3报错中断 4成功
    private Integer status;

    //备注
    private String remark;

    //用户ID
    private Long memberId;

    //手机号
    private String mobile;

    //订单号
    private Long orderId;

    //商品ID
    private Integer goodsId;

    //商家ID
    private Integer merchantId;

    //设备号
    private String deviceCode;

    //优惠券ID
    private Integer couponId;

    //延迟时间 秒
    private Integer delayTime;

    //执行参数 json
//    private String params;

    @TableField(exist = false)
    private List<StepBean> stepList;

    public void nextStep() {
        ++curStep;
    }

    public StepBean queryCurrStep() {
        return stepList.get(curStep);
    }

    //是否已结束
    public boolean isEnd() {
        return status == TaskStatusEnum.CANCEL.getValue() ||
                status == TaskStatusEnum.SUCCESS.getValue() ||
                status == TaskStatusEnum.FAILED.getValue() ||
                curStep + 1 > totalStep;

    }

    public boolean isError() {
        return status == TaskStatusEnum.FAILED.getValue();
    }

    public void setStepList(List<StepBean> stepList) {

        if (stepList == null) {
            return;
        }

        Collections.sort(stepList);

        this.stepList = stepList;
        this.totalStep = stepList.size();
    }

}
